<template>
  <div class="reportFormPage">
    <TopOperation ref="TopOperation" :tabs-list="tabList" :active-val.sync="activeVal" @currentActive="currentActive"
      @reportFormsSubmit="reportFormsSubmit" @printSpecificPart="onPrintSpecificPart" />
    <!-- 水泥混凝土拌合物含气量试验检测记录 -->
    <div v-show="activeVal == 'snhntbhwhql'" v-for="(item, index) in excelTypeMap['snhntbhwhql']"
      :key="'snhntbhwhql' + index">
      <snhntbhwhql ref="snhntbhwhql" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>
    <!-- 水泥混凝土拌合物泌水率检测记录 -->
    <div v-show="activeVal == 'snhntbhwmsl'" v-for="(item, index) in excelTypeMap['snhntbhwmsl']"
      :key="'snhntbhwmsl' + index">
      <snhntbhwmsl ref="snhntbhwmsl" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土坍落度、表观密度试验检测记录表 -->
    <div v-show="activeVal == 'snhntcdbgmd'" v-for="(item, index) in excelTypeMap['snhntcdbgmd']"
      :key="'snhntcdbgmd' + index">
      <snhntcdbgmd ref="snhntcdbgmd" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 混凝土坍落度及扩展度经时损失试验检测记录 -->
    <div v-show="activeVal == 'snhnttldjkzdsyjl'" v-for="(item, index) in excelTypeMap['snhnttldjkzdsyjl']"
      :key="'snhnttldjkzdsyjl' + index">
      <snhnttldjkzdsyjl ref="snhnttldjkzdsyjl" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土抗渗性试验检测记录表 -->
    <div v-show="activeVal == 'snhntksxsy'" v-for="(item, index) in excelTypeMap['snhntksxsy']"
      :key="'snhntksxsy' + index">
      <snhntksxsy ref="snhntksxsy" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土抗弯拉强度试验检测记录表 -->
    <div v-show="activeVal == 'snhntkwlqd'" v-for="(item, index) in excelTypeMap['snhntkwlqd']"
      :key="'snhntkwlqd' + index">
      <snhntkwlqd ref="snhntkwlqd" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土抗压强度试验检测记录表(立方体) -->
    <div v-show="activeVal == 'snhntkyqd'" v-for="(item, index) in excelTypeMap['snhntkyqd']"
      :key="'snhntkyqd' + index">
      <snhntkyqd ref="snhntkyqd" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 混凝土抗压弹性模量试验检测记录表（棱柱体） -->
    <div v-show="activeVal == 'snhntkytxml'" v-for="(item, index) in excelTypeMap['snhntkytxml']"
      :key="'snhntkytxml' + index">
      <snhntkytxml ref="snhntkytxml" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 混凝土凝结时间试验检测记录表 -->
    <div v-show="activeVal == 'snhntnjsj'" v-for="(item, index) in excelTypeMap['snhntnjsj']"
      :key="'snhntnjsj' + index">
      <snhntnjsj ref="snhntnjsj" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 普通混凝土配合比设计试验检测记录表 -->
    <div v-show="activeVal == 'snhntphbsjsyjcjl'" v-for="(item, index) in excelTypeMap['snhntphbsjsyjcjl']"
      :key="'snhntphbsjsyjcjl' + index">
      <snhntphbsjsyjcjl ref="snhntphbsjsyjcjl" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 硬化后水泥混凝土抗渗性能试验检测报告 -->
    <div v-show="activeVal == 'snhntksxnsybg'" v-for="(item, index) in excelTypeMap['snhntksxnsybg']"
      :key="'snhntksxnsybg' + index">
      <snhntksxnsybg ref="snhntksxnsybg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土抗压强度试验检测报告(立方体) -->
    <div v-show="activeVal == 'snhntkyqdbg'" v-for="(item, index) in excelTypeMap['snhntkyqdbg']"
      :key="'snhntkyqdbg' + index">
      <snhntkyqdbg ref="snhntkyqdbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土抗弯拉强度试验检测报告 -->
    <div v-show="activeVal == 'snhntkwlqdbg'" v-for="(item, index) in excelTypeMap['snhntkwlqdbg']"
      :key="'snhntkwlqdbg' + index">
      <snhntkwlqdbg ref="snhntkwlqdbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 普通混凝土配合比设计试验检测报告 -->
    <div v-show="activeVal == 'snhntphbsjsyjcbg'" v-for="(item, index) in excelTypeMap['snhntphbsjsyjcbg']"
      :key="'snhntphbsjsyjcbg' + index">
      <snhntphbsjsyjcbg ref="snhntphbsjsyjcbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 水泥混凝土拌合物含气量试验检测报告 -->
    <div v-show="activeVal == 'snhntbhwhqlbg'" v-for="(item, index) in excelTypeMap['snhntbhwhqlbg']"
      :key="'snhntbhwhqlbg' + index">
      <snhntbhwhqlbg ref="snhntbhwhqlbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>

    <!-- 混凝土凝结时间试验检测报告 -->
    <div v-show="activeVal == 'snhntnjsjbg'" v-for="(item, index) in excelTypeMap['snhntnjsjbg']"
      :key="'snhntnjsjbg' + index">
      <snhntnjsjbg ref="snhntnjsjbg" :excel-id="item" :syType="form.syType" :isPdf="isPdf" />
    </div>



    <backtop-view :sum="activeVal ? excelTypeMap[activeVal].length : 1" @onModPages="onModPages" />
    <statement-select-view ref="statementSelectView" :data-list="tabList" @onShowPreview="onShowPreview" />
    <statement-select-dialog ref="statementSelectDialog" />
  </div>
</template>

<script>
import { getBsDataReport, bsDataReportTb } from "@/api/syrw/bsDataReport";
export default {
  name: "index",
  components: {
    StatementSelectView: () => import("@/views/reportForms/components/StatementSelectView"),
    StatementSelectDialog: () => import("@/views/reportForms/components/StatementSelectDialog"),
    TopOperation: () => import("@/views/reportForms/components/topOperation"), // tab页
    BacktopView: () => import("@/views/reportForms/components/backtopView"), // 回到顶部页

    snhntbhwhql: () => import("@/views/reportForms/snhnt/snhntbhwhql"), //  水泥混凝土拌合物含气量
    snhntbhwhqlbg: () => import("@/views/reportForms/snhnt/snhntbhwhqlbg"), //  水泥混凝土拌合物含气量报告
    snhntbhwmsl: () => import("@/views/reportForms/snhnt/snhntbhwmsl"), //  水泥混凝土拌合物泌水率
    snhntcdbgmd: () => import("@/views/reportForms/snhnt/snhntcdbgmd"), //  水泥混凝土坍落度、表观密度试验检测记录表
    snhnttldjkzdsyjl: () => import("@/views/reportForms/snhnt/snhnttldjkzdsyjl"), //  混凝土坍落度及扩展度经时损失试验检测记录
    snhntksxsy: () => import("@/views/reportForms/snhnt/snhntksxsy"), //  水泥混凝土抗渗性试验检测记录表
    snhntkwlqd: () => import("@/views/reportForms/snhnt/snhntkwlqd"), //  水泥混凝土抗弯拉强度试验检测记录表
    snhntkyqd: () => import("@/views/reportForms/snhnt/snhntkyqd"), //  水泥混凝土抗压强度试验检测记录表(立方体)
    snhntkytxml: () => import("@/views/reportForms/snhnt/snhntkytxml"), //  混凝土抗压弹性模量试验检测记录表（棱柱体）
    snhntnjsj: () => import("@/views/reportForms/snhnt/snhntnjsj"), //  混凝土凝结时间试验检测记录表
    snhntnjsjbg: () => import("@/views/reportForms/snhnt/snhntnjsjbg"), //  混凝土凝结时间试验检测报告
    snhntphbsjsyjcjl: () => import("@/views/reportForms/snhnt/snhntphbsjsyjcjl"), //  普通混凝土配合比设计试验检测记录表
    snhntksxnsybg: () => import("@/views/reportForms/snhnt/snhntksxnsybg"), //  硬化后水泥混凝土抗渗性能试验检测报告
    snhntkyqdbg: () => import("@/views/reportForms/snhnt/snhntkyqdbg"), //  水泥混凝土抗压强度试验检测报告(立方体)
    snhntkwlqdbg: () => import("@/views/reportForms/snhnt/snhntkwlqdbg"), //  水泥混凝土抗弯拉强度试验检测报告
    snhntphbsjsyjcbg: () => import("@/views/reportForms/snhnt/snhntphbsjsyjcbg"), //  普通混凝土配合比设计试验检测报告

  },
  data () {
    return {
      isPdf: false,
      reportId: undefined,
      tabList: [],
      form: {
        syType: "",
        excelTypeList: {}
      },
      activeVal: "",
      // key为excelTypeId， value为 excelId数组
      excelTypeMap: {}
    };
  },
  watch: {
    'activeVal' (val) {
      this.reportFormsSubmit(false);
    }
  },
  created () {
    if (this.$route.query) {
      this.reportId = this.$route.query.reportId;
    }
    this.init();
  },
  methods: {
    // 选择报表
    onPrintSpecificPart () {
      this.$refs.statementSelectView.init();
    },
    //预览
    onShowPreview (tableList) {
      if (!tableList || tableList.length <= 0) {
        return;
      }
      let arr = [];
      this.form.excelTypeList.forEach((item) => {
        if (tableList.includes(item.label)) {
          arr.push({ label: item.label, excelId: item.excelId, syType: this.form.syType, typeSort: item.excelIndex });
        }
      });
      this.$refs.statementSelectDialog.init(arr);
    },
    // 加一页减一页
    async onModPages (modType) {
      // 1先保存
      await this.reportFormsSubmit();
      // 2 加减页
      await this.doModPages(modType);
      // 3 重新加载
      await this.init();
    },
    // 加减页
    async doModPages (modType) {
      const arr = this.excelTypeMap[this.activeVal];
      const data = {
        reportId: this.reportId,
        label: this.activeVal,
        modType: modType,
        excelId: Number(modType) < 0 ? arr[arr.length - 1] : undefined,
        vo: Number(modType) < 0 ? undefined : this.$refs[this.activeVal][arr.length - 1].submitForm()
      };
      await bsDataReportModPages(data);
    },
    // 批量提交
    async reportFormsSubmit (hasTips = true) {
      let arr = [];
      this.tabList.forEach((item) => {
        const refList = this.excelTypeMap[item.val];
        if (!refList || refList.length <= 0) {
          return;
        }
        refList.forEach((element, index) => {
          if (this.$refs[item.val] && this.$refs[item.val][index]) {
            let info = this.$refs[item.val][index].submitForm();
            arr.push({ label: item.val, excelObj: info });
          }
        });
      });
      await bsDataReportTb(arr);
      if (hasTips) {
        this.$modal.msgSuccess("操作成功");
      };
    },
    // 获取记录表和报告表
    async init () {
      if (this.reportId) {
        getBsDataReport(this.reportId).then((response) => {
          this.form = response.data;

          if (!this.form.excelTypeList || this.form.excelTypeList.length == 0) {
            this.form.excelTypeList.push({
              val: undefined,
              name: undefined
            });
            this.tabList = [];
            this.tabList = this.form.excelTypeList;
          } else {
            this.form.excelTypeList.forEach((item) => {
              if (this.excelTypeMap.hasOwnProperty(item.label)) {
                this.excelTypeMap[item.label].push(item.excelId);
              } else {
                this.excelTypeMap[item.label] = [item.excelId];
                this.tabList.push({
                  val: item.label,
                  name: item.excelTypeName
                });
              }
            });
          }

          this.activeVal = this.tabList[0].val;
        });
      } else {
        this.$message.error("报告id不能为空");
      }
    },
    currentActive (val) {
      this.activeVal = val;
    }
  }
};
</script>

<style scoped></style>
